Library.WebControl.Util = (function() {
  var disableFormId = 'Library_WebControl_Util_DisableForm_DivId';
  function isNullOrUndefined(obj) {
    return obj == null || typeof (obj) == "undefined";
  };
  function getBody() {
    var b = (document.body) ? document.body : document.getElementsByTagName('body')[0];
    return b;
  };



  function getObject(obj) {
    if (typeof (obj) == 'string') {
      obj = getObjectById(obj);
    }
    if (obj == null) return null; //obj = {};
    if (obj.IsLibraryControl) return obj;
    obj.IsLibraryControl = true;
    obj.FindFirstChild = FindFirstChild;
    obj.FindLastChild = FindLastChild;
    obj.NextElement = NextElement;
    obj.PreviewElement = PreviewElement;
    obj.Foreach = Foreach;
    obj.FindParent = FindParent;
    obj.SetClass = function(clzz) { return setClass(obj, clzz); };
    obj.RemAtt = RemAtt;
    obj.SetAtt = SetAtt;
    obj.GetAtt = function(a) { if (this.getAttribute) return this.getAttribute(a); return null; };
    obj.GetClass = function() { return getClass(obj); };
    return obj;
  };

  function findElement(tn, clzz, c, f, d) {
    if (isNullOrUndefined(tn)) tn = null;
    if (tn != null)
      tn = tn.toString().toLowerCase();
    if (isNullOrUndefined(clzz)) clzz = null;
    if (clzz != null)
      clzz = clzz.toString().toLowerCase();
    while (c != null) {
      var eq = 0;
      if (tn != null) {
        var name = c.nodeName.toLowerCase();
        if (name == tn) eq++;
      } else {
        eq++;
      }
      if (clzz != null) {
        var clzzName = getClass(c).toLowerCase();
        if (clzzName == clzz) eq++;
      } else {
        eq++;
      }

      if (eq == 2)
        return getObject(c);
      if (d == true) {
        c = getObject(c);
        var resObj = null;
        c.Foreach(function(s, e) {
          var cld = findElement(tn, clzz, s, function(s) { return f(s); }, d);
          if (cld != null) {
            resObj = getObject(cld);
            e.Break = true;
          }
        });
        if (resObj != null)
          return resObj;
      }
      c = f(c);
    }
    return null;
  };
  function NextElement(tn, clazz) {
    return findElement(tn, clazz, this.nextSibling, function(n) { return n.nextSibling; });
  };
  function PreviewElement(tn, clazz) {
    return findElement(tn, clazz, this.previousSibling, function(n) { return n.previousSibling; });
  };
  function FindLastChild(tn, clazz, deep) {
    return findElement(tn, clazz, this.lastChild, function(n) { return n.previousSibling; }, deep);
  };
  function FindFirstChild(tn, clazz, deep) {
    return findElement(tn, clazz, this.firstChild, function(n) { return n.nextSibling; }, deep);
  };
  function FindParent(tn, clazz) {
    return findElement(tn, clazz, this.parentNode, function(n) { return n.parentNode; });
  };
  function Foreach(f) {
    var j = -1;
    for (var i = 0; i < this.childNodes.length; i++) {
      var n = this.childNodes[i];
      if (n.nodeType == 1) {
        j++;
        var e = { ItemIndex: i, ElementIndex: j, Break: false };
        f(getObject(n), e);
        if (e.Break) break;
      }
    }
    return this;
  };
  function RemAtt(att) {
    if (this.removeAttribute)
      this.removeAttribute(att);
    return this;
  };
  function SetAtt(att, value) {
    if (this.setAttribute)
      this.setAttribute(att, value);
    return this;
  };


  function getObjectById(id) {
    if (document.getElementById) {
      return document.getElementById(id);
    }
    else
      if (document.all) {
      return document.all[id];
    }
    else
      if (document.anchors && document.anchors.length && document.anchors.length > 0 && document.anchors[0].x) {
      for (var i = 0; i < document.anchors.length; i++) {
        if (document.anchors[i].name == id) {
          return document.anchors[i]
        }
      }
    }
  };

  function getObjectSize(obj) {
    obj = getObject(obj);
    return {
      W: obj.clientWidth,
      H: obj.clientHeight
    };
  };

  function getPosition(obj) {
    var o = getObject(obj);
    var l = t = 0;
    if (o.offsetParent) {
      l = o.offsetLeft;
      t = o.offsetTop;
      while (o = o.offsetParent) {
        //if (o.style && o.style.position != "absolute") {
        l += o.offsetLeft;
        t += o.offsetTop;
        //}else{
        //break;
        //}
      }
    }

    return {
      X: l,
      Y: t
    };
  };
  function getRelativePosition(obj) {
    var o = getObject(obj);
    var l = t = 0;
    if (o.offsetParent) {
      l = o.offsetLeft;
      t = o.offsetTop;
      while (o = o.offsetParent) {
        //if (o.style && o.style.position != "absolute") {
        if (stylePosition(o) != "absolute") {
          l += o.offsetLeft;
          t += o.offsetTop;
        }
        else {
          break;
        }
      }
    }

    return {
      X: l,
      Y: t
    };
  };
  function getScrollOffset() {
    var x, y;
    var e = document.documentElement;
    var b = getBody();
    if (self.pageYOffset) {
      y = self.pageYOffset;
      x = self.pageXOffset;
    }
    else
      if (e && e.scrollTop) {
      y = e.scrollTop;
      x = e.scrollLeft;
    }
    else
      if (b) {// all other Explorers
      y = b.scrollTop;
      x = b.scrollLeft;
    }
    return [x, y]

  };
  function moveToCenter(parrent, child) {
    var p = getObject(parrent);
    var c = getObject(child);
    var ps = getObjectSize(p);
    var pp = getPosition(p);
    var cs = getObjectSize(c);
    var ps2 = {
      W: ps.W / 2,
      H: ps.H / 2
    };
    var pscp = {
      X: pp.X + ps2.W,
      Y: pp.Y + ps2.H
    };

    var p = {
      L: pscp.X - cs.W / 2,
      T: pscp.Y - cs.H / 2
    };
    c.style.left = p.L + 'px';
    c.style.top = p.T + 'px';
  };
  function getDocumentSize() {
    var e = document.documentElement;
    var b = getBody();
    var h, w;
    if (e && e.scrollHeight) {
      h = e.scrollHeight;
    } else {
      h = (b.scrollHeight > b.offsetHeight) ? b.scrollHeight : b.offsetHeight;
    }
    w = (b.scrollWidth > b.offsetWidth) ? b.scrollWidth : b.offsetWidth;
    //var w = (e && e.scrollWidth) ? e.scrollWidth : (b.scrollWidth > b.offsetWidth) ? b.scrollWidth : b.offsetWidth;
    //var h = (e && e.scrollHeight) ? e.scrollHeight : (b.scrollHeight > b.offsetHeight) ? b.scrollHeight : b.offsetHeight;
    return {
      W: w,
      H: h
    };
  };
  function getWindownViewSize() {
    if (window.innerWidth) {
      return {
        W: window.innerWidth,
        H: window.innerHeight
      };
    }
    else {
      var e = document.documentElement;
      if (e && e.clientWidth) {
        return {
          W: e.clientWidth,
          H: e.clientHeight
        };
      }
      else {
        var b = getBody();
        return {
          W: b.clientWidth,
          H: b.clientHeight
        };
      }
    }
  };
  function stylePosition(obj) {

    var x = getObject(obj);
    if (x.style && x.style.position != "") return x.style.position;
    return getStyle(x, "position");

    //var styleProp = "position";
    //if (x.currentStyle)
    //  var y = x.currentStyle[styleProp];
    //else if (window.getComputedStyle)
    //  var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
    //return y;
  };
  function getEvent(e) {
    var evt = (e) ? e : window.event;
    return evt;
  };
  function getKey(evt) {
    evt = getEvent(evt);
    return (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
  };

  function mouseCoords(ev) {
    ev = getEvent(ev);
    if (ev.pageX || ev.pageY) {
      return {
        X: ev.pageX,
        Y: ev.pageY
      };
    }
    var b = getBody();
    return {
      X: ev.clientX + b.scrollLeft - b.clientLeft,
      Y: ev.clientY + b.scrollTop - b.clientTop
    };
  };
  function moveToCenterScreen(obj) {
    obj = getObject(obj);
    if (obj) {
      var size = getObjectSize(obj);
      var winSize = getWindownViewSize(); // not include scroll 
      var docSize = getDocumentSize();
      var l = (docSize.W - size.W) / 2;
      var t = (winSize.H - size.H) / 2;
      if (l < 0) {
        t = t - l;
        l = 0;
      }
      if (t < 0)
        t = 0;
      var sc = getScrollOffset();
      l += sc[0];
      t += sc[1];
      var absParrent = getAbsParrent(obj);
      if (absParrent != null) {
        var posAbsParent = getPosition(absParrent);
        l = l - posAbsParent.X;
        t = t - posAbsParent.Y;
      }
      obj.style.left = l + 'px';
      obj.style.top = t + 'px';
      obj.style.position = 'absolute';
    }
  };
  function moveToTopCenterScreen(obj) {
    obj = getObject(obj);
    if (obj) {
      var size = getObjectSize(obj);
      var docSize = getDocumentSize();
      var l = (docSize.W - size.W) / 2;
      //var t = 0;//(docSize.H - size.H) / 2;
      if (l < 0) {
        //t = t - l;
        l = 0;
      }
      //if (t < 0)
      //  t = 0;
      var sc = getScrollOffset();
      l += sc[0];
      var t = sc[1];
      var absParrent = getAbsParrent(obj);
      if (absParrent != null) {
        var posAbsParent = getPosition(absParrent);
        l = l - absParrent.X;
        t = t - absParrent.Y;
      }
      obj.style.position = 'absolute';
      obj.style.left = l + 'px';
      obj.style.top = t + 'px';
      //      if (obj.style.setAttribute)
      //				obj.style.setAttribute("position", "fixed");
      //      else
      //obj.style.position = 'fixed';
    }
  };
  function getAbsParrent(obj) {
    obj = getObject(obj);
    if (obj) {
      var parrentObj = obj.parentNode;
      while (parrentObj != null) {
        var stylePos = stylePosition(parrentObj);
        //if (parrentObj.style) {
        //  if (parrentObj.style.position == "absolute" || parrentObj.style.position == "relative") {
        //    return parrentObj;
        //  }
        //}
        if (stylePos == "absolute" || stylePos == "relative") return parrentObj;
        parrentObj = parrentObj.parentNode
      }
    }
    return null;
  }

  function setClass(obj, clazz) {
    obj = getObject(obj);
    obj.setAttribute("class", clazz);
    obj.className = clazz;
    return obj;
  };
  function getClass(obj) {
    obj = getObject(obj);
    var c = "";
    if (obj.getAttribute) {
      c = obj.getAttribute("class");
    }
    if (c == null || c == "") {
      c = obj.className;
    }
    if (c == null) return "";
    return c;
  };
  function maxParentZIndex(obj) {
    var max = 0;
    if (obj) {
      //      if (obj.zIndex)
      //        max = obj.zIndex;
      //      else {
      //        if (obj.style) {
      //          max = parseInt(obj.style.zIndex);
      //          if (isNaN(max))
      //            max = 0;

      //        }
      //      }
      max = findZIndex(obj);
      var p = obj.parentNode;
      var maxP = maxParentZIndex(p);
      if (max > maxP) return max;
      return maxP;
    }
    return max;
  };
  function getStyle(obj, name) {
    //obj = getObjectById(obj);
    if (obj.currentStyle) return obj.currentStyle[name];
    var dv = document.defaultView || window;
    if (dv.getComputedStyle && obj.nodeType == 1) {
      return dv.getComputedStyle(obj, null).getPropertyValue(name);
    }
    return null;
  };
  function findZIndex(obj) {
    if (!obj) return 0;
    if (obj.zIndex) return obj.zIndex;
    if (obj.style) {
      var z = parseInt(obj.style.zIndex);
      if (isNaN(z))
        z = 0;
      return z;
    }
    var z = parseInt(getStyle(obj, "z-index"));
    //if (z != null) {
    // z = parseInt(z);
    //}
    if (isNaN(z))
      z = 0;
    return z;
  };
  function findMaxZindex(obj) {
    var max = 0;
    if (obj) {
      //      if (obj.zIndex)
      //        max = obj.zIndex;
      //      else {
      //        if (obj.style) {
      //          max = parseInt(obj.style.zIndex);
      //          if (isNaN(max))
      //            max = 0;

      //        }
      //      }
      max = findZIndex(obj);
      for (var i = 0; i < obj.childNodes.length; i++) {
        var child = findMaxZindex(obj.childNodes[i]);
        if (child > max)
          max = child;
      }
    }
    return max;
  };
  function trimLeft(text) {
    for (var i = 0; i < text.length; i++) {
      switch (text.charAt(i)) {
        case "\n":
        case "\r":
        case "\t":
        case " ":
          break;
        default:
          return text.substr(i, text.length - i);
      }
    }

    return "";
  };
  function trimRight(text) {
    for (var i = text.length - 1; i >= 0; i--) {
      switch (text.charAt(i)) {
        case "\n":
        case "\r":
        case "\t":
        case " ":
          break;
        default:
          return text.substr(0, i + 1);
      }
    }
    return "";
  };
  function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
      return null;
    }

    if (start == -1) return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
  };
  function toArrayString() {
    var result = "";
    //if (isNullOrUndefined(args)) {
    //  args = arguments;
    //} else if (arguments.length > 1) {
    //  args = arguments;
    //}
    var args = new Array();
    for (var i = 0; i < arguments.length; i++) {
      var a = arguments[i];
      var t = typeof (a);
      switch (t) {
        case "object":
          //check if array
          if (a instanceof Array) {
            for (var j = 0; j < a.length; j++) {
              args.push(a[j]);
            }
          } else {
            args.push(a.toString());
          }
          break;
        default:
          args.push(a.toString());
          break;
      }
    }

    var ar = new Array();
    for (var i = 0; i < args.length; i++) {
      var a = args[i].toString();
      result += a.length + "|" + a;
    }
    return result;
  };
  function handlePress(e, k, f) {
    if (getKey(e) == k) {
      if (typeof (f) == "function") {
        f();
      }
      var ev = getEvent(e);
      ev.cancelBubble = true;
      if (ev.preventDefault) ev.preventDefault();
      return false;
    }
    return true;
  };
  return {
    /*
    Constant key
    */
    Keys: { ENTER: 13, F4: 115, ARROW_UP: 38, ARROW_DOWN: 40, ESCAPE: 27, TAB: 9 },
    ToArrayString: toArrayString,
    GetSize: getObjectSize,
    GetPosition: getPosition,
    GetRelativePosition: getRelativePosition,
    GetDocumentSize: getDocumentSize,
    MoveToCenter: moveToCenter,
    MoveToCenterScreen: moveToCenterScreen,
    MoveToTopCenterScreen: moveToTopCenterScreen,
    GetEvent: getEvent,
    GetKey: getKey,
    GetMousePosition: mouseCoords,
    GetObjectById: getObject,
    GetAbsParrent: getAbsParrent,
    GetClass: getClass,
    GetCookie: getCookie,
    TrimLeft: trimLeft,
    TrimRight: trimRight,
    SetClass: setClass,
    HandlePress: handlePress,
    GetParentScrollOffset: function(obj) {
      var sc = [0, 0];
      var obj = getObject(obj);
      var p = obj.parentNode;
      while (p != null) {
        if (p.scrollLeft) {
          sc[0] += p.scrollLeft;
        }
        if (p.scrollTop) {
          sc[1] += p.scrollTop;
        }
        p = p.parentNode;

      }
      return sc;
    },

    SetCookie: function(name, value, expires, path, domain, secure) {
      var today = new Date();
      today.setTime(today.getTime());
      if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
      }

      var expires_date = new Date(today.getTime() + (expires));
      document.cookie = name + '=' + escape(value) +

				((expires) ? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString()

				((path) ? ';path=' + path : '') +

				((domain) ? ';domain=' + domain : '') +

				((secure) ? ';secure' : '');
    },
    DeleteCookie: function(name, path, domain) {
      if (getCookie(name))
        document.cookie = name + '=' +
			((path) ? ';path=' + path : '') +

			((domain) ? ';domain=' + domain : '') +

			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    },
    DisableForm: function() {
      var d = getObjectById(disableFormId);
      if (!d) {
        var b = getBody();
        if (b) {
          d = document.createElement('div');
          d.style.filter = 'alpha(opacity=50)';
          d.style.opacity = '0.5';
          d.style.backgroundColor = 'gray';
          d.style.top = '0px';
          d.style.left = '0px';
          d.style.zIndex = Library.WebControl.Util.GetMaxZindex() + 1;
          d.setAttribute('id', disableFormId);
          d.style.position = 'fixed';
          b.appendChild(d);
        }
      }
      var docSize = getDocumentSize();
      d.style.height = docSize.H + 'px';
      d.style.width = docSize.W + 'px';
      d.style.visibility = 'visible';
    },
    SetInputEnable: function(input, enable) {
      if (enable) input.removeAttribute("readonly");
      else input.setAttribute("readonly", "readonly");
    },
    EnableForm: function() {
      var d = getObjectById(disableFormId);
      if (d) {
        d.style.visibility = 'hidden';
      }
    },
    FindFirstChild: function(p, tn, clzz, deep) {
      p = getObject(p);
      return p.FindFirstChild(tn, clzz, deep);
    },
    FindLastChild: function(p, tn, clzz, deep) {
      p = getObject(p);
      return p.FindLastChild(tn, clzz, deep);
    },

    FindParent: function(c, tn, clzz) {
      c = getObject(c);
      return c.FindParent(tn, clzz);
    },
    EvalScriptAndHtml: function(h, div) {
      div = getObject(div);
      while (div.lastChild) {
        div.removeChild(div.lastChild);
      }
      //var root = document.body || document.documentElement;
      var d = document.createElement('div');
      d.innerHTML = '<table><tr><td>' + h + '</table></tr></td>';
      var sc = d.getElementsByTagName('script');
      if (sc) {
        var index = 0;
        while (sc.length > index) {
          var script = sc[index];
          var scr = script.getAttribute("src");
          if (!scr || scr == null || scr == "") {
            script.parentNode.removeChild(script);
            if (script.innerHTML && script.innerHTML != null) {
              eval(script.innerHTML.replace("<!--", ""));
            }
          }
          else {
            index++;
          }
        }
      }
      while (d.tagName.toUpperCase() != 'TD') {
        d = d.firstChild;
      }
      while (d.firstChild) {
        var fc = d.firstChild;
        div.appendChild(fc);
      }

    },
    ShowLoadingPanel: function(o) {
      if (o) {
        if (o.Parrent) {
          o.Parrent = getObject(o.Parrent);
          if (!o.LoadingId) {
            o.LoadingId = '_div_panel_loading';
          }
          if (!o.LoadingClass) {
            o.LoadingClass = 'sploadingpanel';
          }
          var d = getObjectById(o.LoadingId);
          if (!d) {
            d = document.createElement('div');
            d.setAttribute('id', o.LoadingId);
            d.style.position = 'absolute';
            var b = getBody();
            b.appendChild(d);
          }
          setClass(d, o.LoadingClass);
          //d.setAttribute('class', o.LoadingClass);
          //d.setAttribute('classname', o.LoadingClass);
          moveToCenter(o.Parrent, d);
          d.style.visibility = 'visible';
          d.style.zIndex = maxParentZIndex(o.Parrent) + 1;
        }
      }
    },
    HideLoadingPanel: function(id) {
      if (!id) {
        id = '_div_panel_loading';
      }
      var d = getObjectById(id);
      if (d) {
        d.style.visibility = 'hidden';
      }

    },
    GetMaxZindex: function() {
      var b = getBody();
      var max = findMaxZindex(b);
      if (max == 0) return 1000;
      return max;
      //return findMaxZindex(b) + 10;
    },
    Trim: function(t) {
      return trimRight(trimLeft(t));
    },

    HandleEnterPress: function(e, f) {
      return handlePress(e, 13, f);
    },

    ParseArrResponse: function(t) {
      var arr = new Array();
      if (t == "") return arr;
      var l = "";
      for (var i = 0; i < t.length; i++) {
        var c = t.charAt(i);
        if (c == '|') {
          var rl = parseInt(l);
          var st = i + 1;
          var et = st + rl;
          var data = t.substring(st, et);
          arr.push(data);
          i = et - 1;
          l = "";
        }
        else {
          l += c;
        }
      }
      return arr;
    },
    ParseAjaxResponse: function(t) {
      var r = new Object();
      r.C = t.charAt(0);
      if (r.C == 'F') {//fail
        r.T = t.substring(1);
        r.D = '';
      }
      else {
        var l = '';
        for (var i = 1; i < t.length; i++) {
          var c = t.charAt(i);
          if (c == '|') {
            break;
          }
          else {
            l += c;
          }
        }
        var rl = parseInt(l);
        var st = 2 + l.length;
        r.T = t.substring(st, st + rl);
        r.D = t.substring(st + rl);
      }
      return {
        IsValid: r.C == "S",
        Result: r.T,
        Data: r.D
      };
    }


  };
} ());
var _FO = Library.WebControl.Util.GetObjectById;